<?php

namespace app\data\controller;
use think\admin\Controller;
use think\admin\helper\QueryHelper;
use app\data\model\DataDeviceOta;
use app\data\service\DeviceService;
/**
 * 固件升级管理
 * @class Hotel
 * @package app\data\controller
 */
class Ota extends Controller
{
    /**
     * 固件升级管理
     * @menu true
     * @auth true
     */
    public function index()
    {
        DataDeviceOta::mQuery()->layTable(function () {
            $this->title = '固件升级管理';
        }, function (QueryHelper $query) {
            $query->where(['is_deleted' => 0]);
            // 数据列表搜索过滤
            $query->dateBetween('create_at#create_at');
        });
    }
    /**
     * 获取升级包地址
     * @auth true
     */
    public function add()
    {
        if ($this->request->isGet()) {
            return $this->fetch();
        }else{
            $data=[
                'FileSuffix'=>input('param.file_suffix')
            ];
            $mod = new DeviceService();
            $response = $mod->GenerateOTAUploadURL($data);
            if($response['err_code'] == 1){
                $this->error($response['data']);
            }else{
                $res = $response['data'];
                if($res->body->code !=''){
                    $this->error($res->body->errorMessage);
                }else{
                    $backData=[
                        'firmware_name'=>input('post.firmware_name'),
                        'version'=>input('post.version'),
                        'firmware'=>input('post.firmware'),
                        'sign_method'=>input('post.sign_method'),
                        'firmware_desc'=>input('post.firmware_desc'),
                        'Udi'=>input('post.Udi'),
                        'file_suffix'=>input('post.file_suffix'),
                        'firmware_url'=>$res->body->data->firmwareUrl,
                    ];
                    DataDeviceOta::mk()->insert($backData);
                    $this->success('添加成功');
                }
            }

        }
    }
}
